package com.hxx.controller;

import com.hxx.domain.TUser;
import com.hxx.service.UserService;
import com.hxx.service.UserServiceImpl;
import com.hxx.utils.DBUtils;
import com.hxx.utils.ViewBaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/user")
public class UserController1 extends ViewBaseServlet {

    private UserService userService = new UserServiceImpl();
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println(request.getRequestURI());
        //获取一个标识参数
        String flag = request.getParameter("flag");
        if ("add".equals(flag)) {
            //添加用户
            //获取用户提交的参数
            String username = request.getParameter("username");
            String sex = request.getParameter("sex");
            String pwd = request.getParameter("pwd");
            //封装成一个TUser对象
            TUser tUser = new TUser();
            tUser.setUsername(username);
            tUser.setSex(sex);
            tUser.setPwd(pwd);
            //调用业务方法
            userService.save(tUser);
        }else if ("del".equals(flag)) {
            //删除用户
            //获取用户提交的参数
            int id = Integer.parseInt(request.getParameter("id"));
            //调用业务方法
            userService.delete(id);
        } else if ("update".equals(flag)) {
           //更新用户
           //获取用户提交的参数
            int id = Integer.parseInt(request.getParameter("id"));
            String username = request.getParameter("username");
            String sex = request.getParameter("sex");
            String pwd = request.getParameter("pwd");
            //封装成一个TUser对象
            TUser tUser = new TUser();
            tUser.setId(id);
            tUser.setUsername(username);
            tUser.setSex(sex);
            tUser.setPwd(pwd);
            //调用业务方法
            userService.update(tUser);
        } else if ("data".equals(flag)) {
            //更新用户的页面数据
            //获取用户提交的参数
            int id = Integer.parseInt(request.getParameter("id"));
            //调用业务方法
            TUser tUser = userService.findById(id);
            //将查询到的用户对象存储到request域中
            request.setAttribute("user", tUser);
            //转发到更新用户的页面
            processTemplate("update", request, response);
            return;
        }else if ("queryAll".equals(flag)) {
            //查询所有用户
        }
        //查询所有用户
        //调用业务方法
        List<TUser> list = userService.findAll();
        //将查询到的用户列表存储到request域中
        request.setAttribute("list", list);
        //转发到展示页面
        processTemplate("show", request, response);
    }
}
















































